package jms.topic;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class AppConsumer {
		
		private static final String url = "tcp://localhost:61616";
	    private static final String topicName = "topic-test";
	    
	    public static void main(String[] args) throws JMSException {
			//创建connectionFactory
	    	ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
	    	//创建连接Connection
	    	Connection connection = connectionFactory.createConnection();
		    //启动连接
	    	connection.start();
	    	//创建会话
	    	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    	//创建一个目标
	    	Destination destination = session.createTopic(topicName);
	    	//创建一个消费者
	    	MessageConsumer consumer = session.createConsumer(destination);
	    	//创建一个监听器
	    	consumer.setMessageListener(new MessageListener() {
				
				@Override
				public void onMessage(Message message) {
					TextMessage textMessage = (TextMessage) message;
					try {
						System.out.println("接收消息"+textMessage.getText());
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
	    	//关闭连接
	    	//connection.close();
	    }


}
